import requests
import urllib.request
from lxml import etree
import os
import re
def parse_page(url):
    headers = {
        "User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36"
    }
    response = requests.get(url,headers=headers)
    text = response.content.decode('utf-8')

    html = etree.HTML(text)
    imgs = html.xpath("//div[@class='page-content text-center']//img[@class!='gif']")
    for img in imgs:
        img_url = img.get('data-original')
        suffix = os.path.splitext(img_url)[1]  # 获取图片的后缀名
        alt = img.get('alt')
        alt = re.sub(r'[？\?\.。！!]','',alt) #windows中不需要特殊符号作为文件名  这里将特殊符号替换为空
        filename = alt+suffix
        # print(alt)
        urllib.request.urlretrieve(img_url,"images/"+filename)

def main():
    for page in  range(1,51):
        url = "http://www.doutula.com/photo/list/?page=%d" % page
        parse_page(url)
if __name__ == '__main__':
    main()